$(document).ready(function(){
    $('.sym_light_menu_admin_items ul').sortable({
        handle: '.sort',
        placeholder: 'ui-state-highlight',
        start: function(){
            $(this).find('ul').hide();
        },
        stop:function(){
            $(this).find('ul').show();
        },
        update: function(){
            var sort=new Array();
            $('.sym_light_menu_admin_items .id').each(function(){
                sort.push($(this).text());
            });
            var menu_id=$('.sym_light_menu_admin_items .menu_id').text();
            $.ajax({
                data:{
                    'symbiont': 'light_menu-admin.db_sort',
                    'sort': sort,
                    'menu_id': menu_id
                }
            });
        }
    });
    //Add
    $('.sym_light_menu_admin_items .symbiosis-button-add').click(function(){
        if($('#sym_light_menu_admin_items_add').length){
            $('#sym_light_menu_admin_items_add').dialog('open');
        }
        else{
            $('#windows').append('<div id="sym_light_menu_admin_items_add" title="Menu item adding"></div>');
            $('#sym_light_menu_admin_items_add').dialog({
                modal: true,
                width: 270,
                position: ['center', 100],
                open: function(event, ui) {
                    var menu_id=$('.sym_light_menu_admin_items .menu_id').text();
                    $.ajax({
                        data:{
                            symbiont: 'light_menu-admin.item_add.'+menu_id
                        },
                        success:function(r){
                            $('#sym_light_menu_admin_items_add').html(r).sym();
                            $('#sym_light_menu_admin_items_add .radio').button();
                            $('#sym_light_menu_admin_items_add .radio').change(function(){
                                if($(this).hasClass('radio_link')){
                                    $(this).parent().find('.link').attr('disabled', false);
                                    $(this).parent().parent().find('.page').attr('disabled', true);
                                }
                                if($(this).hasClass('radio_page')){
                                    $(this).parent().find('.page').attr('disabled', false);
                                    $(this).parent().parent().find('.link').attr('disabled', true);
                                }
                            });
                            $('#sym_light_menu_admin_items_add .tabs')
                            .tabs();
                        },
                        dataType: 'html'
                    });
                },
                buttons:{
                    "Cancel":function(){
                        $(this).dialog("close");
                        $(this).html('');
                    },
                    "Ok":function(){
                        var r={};
                        r['symbiont']='light_menu-admin.db_item_add';
                        r['menu_id']=$('.sym_light_menu_admin_items .menu_id').text();
                        r['parent_id']=$('#sym_light_menu_admin_items_add .parent').val();
                        if($('#sym_light_menu_admin_items_add .radio_page').attr('checked')){
                            r['page_id']=$('#sym_light_menu_admin_items_add .page').val();
                        }
                        else if($('#sym_light_menu_admin_items_add .radio_link').attr('checked')){
                            r['link']=$('#sym_light_menu_admin_items_add .link').val();
                        }
                        t=false;
                        $('#sym_light_menu_admin_items_add .title').each(function(){
                            val=$(this).val();
                            if(val&&!t){
                                r['title']={};
                                t=true;
                            }
                            if(val) r['title'][$(this).parent().parent().find('.lng_id').text()]=$(this).val();
                        });
                        var d=$(this);
                        $('#sym_light_menu_admin_items_add .message>div').removeClass('ui-state-error').html('');
                        $.ajax({
                            data:r,
                            success:function(r){
                                if(r.ok!=undefined){
                                    d.dialog("close");
                                    d.html('');
                                    location.reload(true);
                                }
                                else if(r.error!=undefined){
                                    $('#sym_light_menu_admin_items_add .message>div').addClass('ui-state-error').html(r.error);
                                }
                            }
                        });
                    }
                }
            });
        }
    });
    //Edit
    $('.sym_light_menu_admin_items .edit').click(function(){
        var t=this;
        if($('#sym_light_menu_admin_items_edit').length){
            $('#sym_light_menu_admin_items_edit').dialog('open');
        }
        else{
            $('#windows').append('<div id="sym_light_menu_admin_items_edit" title="Menu editing"></div>');
            $('#sym_light_menu_admin_items_edit').dialog({
                modal: true,
                width: 270,
                position: ['center', 100],
                open: function(event, ui) {
                    var id=$(t).parent().find('.id').text();
                    var menu_id=$('.sym_light_menu_admin_items .menu_id').text();
                    $.ajax({
                        data:{
                            symbiont: 'light_menu-admin.item_edit.'+menu_id+'.'+id
                        },
                        success:function(r){
                            $('#sym_light_menu_admin_items_edit').html(r).sym();
                            $('#sym_light_menu_admin_items_edit .radio').button();
                            $('#sym_light_menu_admin_items_edit .radio').change(function(){
                                if($(this).hasClass('radio_link')){
                                    $(this).parent().find('.link').attr('disabled', false);
                                    $(this).parent().parent().find('.page').attr('disabled', true);
                                }
                                if($(this).hasClass('radio_page')){
                                    $(this).parent().find('.page').attr('disabled', false);
                                    $(this).parent().parent().find('.link').attr('disabled', true);
                                }
                            });
                            $('#sym_light_menu_admin_items_edit .tabs')
                            .tabs();
                        },
                        dataType: 'html'
                    });
                },
                buttons:{
                    "Cancel":function(){
                        $(this).dialog("close");
                        $(this).html('');
                    },
                    "Ok":function(){
                        var r={};
                        r['symbiont']='light_menu-admin.db_item_edit';
                        r['menu_id']=$('.sym_light_menu_admin_items .menu_id').text();
                        r['id']=$(t).parent().find('.id').text();
                        r['parent_id']=$('#sym_light_menu_admin_items_edit .parent').val();
                        if($('#sym_light_menu_admin_items_edit .radio_page').attr('checked')){
                            r['page_id']=$('#sym_light_menu_admin_items_edit .page').val();
                        }
                        else if($('#sym_light_menu_admin_items_edit .radio_link').attr('checked')){
                            r['link']=$('#sym_light_menu_admin_items_edit .link').val();
                        }
                        k=false;
                        $('#sym_light_menu_admin_items_edit .title').each(function(){
                            val=$(this).val();
                            if(val&&!k){
                                r['title']={};
                                k=true;
                            }
                            if(val) r['title'][$(this).parent().parent().find('.lng_id').text()]=$(this).val();
                        });
                        var d=$(this);
                        $('#sym_light_menu_admin_items_edit .message>div').removeClass('ui-state-error').html('');
                        $.ajax({
                            data:r,
                            success:function(r){
                                if(r.ok!=undefined){
                                    d.dialog("close");
                                    d.html('');
                                    location.reload(true);
                                }
                                else if(r.error!=undefined){
                                    $('#sym_light_menu_admin_items_edit .message>div').addClass('ui-state-error').html(r.error);
                                }
                            }
                        });
                    }
                }
            });
        }
    });
    //Delete
    $('.sym_light_menu_admin_items .delete').click(function(){
        t=$(this);
        if($('#sym_light_menu_admin_items_delete').length){
            $('#sym_light_menu_admin_items_delete').dialog('open');
        }
        else{
            $('#windows').append('<div id="sym_light_menu_admin_items_delete" title="Menu item deleting"></div>');
            $('#sym_light_menu_admin_items_delete').dialog({
                modal: true,
                width: 270,
                position: ['center', 100],
                open: function(event, ui) {
                    var id=$(t).parent().find('.id').text();
                    $.ajax({
                        data:{
                            symbiont: 'light_menu-admin.item_delete.'+id
                        },
                        success:function(r){
                            $('#sym_light_menu_admin_items_delete').html(r).sym();
                        },
                        dataType: 'html'
                    });
                },
                buttons:{
                    "Cancel":function(){
                        $(this).dialog("close");
                        $(this).html('');
                    },
                    "Ok":function(){
                        var id=$(t).parent().find('.id').text();
                        var menu_id=$('.sym_light_menu_admin_items .menu_id').text();
                        var d=$(this);
                        $.ajax({
                            data:{
                                symbiont: 'light_menu-admin.db_item_delete',
                                id: id,
                                menu_id: menu_id
                            },
                            success:function(r){
                                d.dialog("close");
                                d.html('');
                                t.parent().slideUp();
                            }
                        });
                    }
                }
            });
        }
    });
});